 %% @author Administrator
%% @doc @todo Add description to 群功能.

%%@author wangming
%%2017年9月4日
-module(group_info).
-include("common.hrl").
-export([req_create_group/4,req_dissolve_group/4,req_join_group/4,req_updata_group_name/5,
		 req_delete_group_members/4,req_get_group_members_list/3,req_quit_group/3,req_get_group_room_list/3]).


%%创建牌友群 传入群名字
req_create_group(Aid,GroupName,Sid,Seq)->
	case db:insert(#group_info{group_name=util:to_binary(GroupName),create_aid=Aid}) of
		[#group_info{id=GroupId}|_]->
				db:insert(#group_members{state=1,group_id=GroupId,accid=Aid});
		_->skip
	end,
	ok.
%%加入牌友群
req_join_group(Aid,GroupId,Sid,Seq)->
	case db:dirty_get(group_info, GroupId) of
		[#group_info{}|_]->
			db:insert(#group_members{group_id=GroupId,accid=Aid});
		_->skip
	end.
req_consent_group(Aid,Seq,TagAid,TagGroupId)->
	case db:dirty_match(group_members,#group_members{state=0,accid=TagAid,group_id=TagGroupId,_='_'}) of
		[GroupMembers = #group_members{}|_]->
			db:dirty_put(GroupMembers#group_members{state=1});
		_->skip
	end.
%%更改群名字
req_updata_group_name(Aid,GroupId,GroupName,Sid,Seq)->
	case db:dirty_get(group_info, GroupId) of
		[Group = #group_info{group_name=OldGroupName}|_]->
				case string:equal(GroupName, util:to_string(OldGroupName)) of
					false->
						db:dirty_put(Group#group_info{group_name=GroupName});
					_->skip
				end;
		_->skip
	end.
%%解散群
req_dissolve_group(Aid,GroupId,Sid,Seq)->
	case db:dirty_get(group_info, GroupId) of
		[Group = #group_info{create_aid=Aid}|_]->
			db:dirty_put(Group#group_info{state=1}),
			case db:dirty_get(group_members, GroupId, #group_members.group_id) of
				MembersList when is_list(MembersList) andalso length(MembersList)>0->
					Fun = fun(#group_members{id=MemberId})->
								  db:dirty_del(group_members, MemberId)
						  end,
					lists:foreach(Fun, MembersList);
				_->skip
			end;
		_->skip
	end.
%%群主删除成员
req_delete_group_members(Aid,GroupId,TagAccId,Seq)->
	case db:dirty_get(group_info, GroupId) of
		[#group_info{create_aid=Aid}|_]->
			case db:dirty_match(group_members, #group_members{accid=Aid,group_id=GroupId,_='_'}) of
				[#group_members{id=ID}|_]->
					db:dirty_del(group_members, ID);
				_->skip
			end;
		_->skip
	end.
%%请求退出群
req_quit_group(Aid,GroupId,Seq)->
	case db:dirty_match(group_members, #group_members{accid=Aid,group_id=GroupId,_='_'}) of
		[#group_members{id=ID}|_]->
			db:dirty_del(group_members, ID);
		_->skip
	end.
%%获得群成员列表
req_get_group_members_list(Aid,Sid,Seq)->
	case db:dirty_get(group_members, Aid, #group_members.accid) of
		GroupList when is_list(GroupList) andalso length(GroupList)>0->
			Fun = fun(#group_members{group_id=GroupId},Acc)->
						  case db:dirty_get(group_info, GroupId) of
							  [#group_info{create_aid=CreateAid,group_name=GroupName}|_]->
								  case db:dirty_get(group_members, GroupId, #group_members.group_id) of
									  GroupMembersList when is_list(GroupMembersList) andalso length(GroupMembersList)->
										  Fun1 = fun(#group_members{state=1,accid=AccId},Acc1)->
														 case db:dirty_get(account,AccId) of
															 [#account{name=Name,head_img=HeadImg}|_]->
																 Acc1 ++ [{AccId,Name,util:to_list(HeadImg),CreateAid}];
															 _->Acc1
														 end
												 end,
										  GroupImgList = lists:foldl(Fun1,[],GroupMembersList),
										  Acc ++ [{GroupName,length(GroupImgList),GroupImgList}];
									  _->Acc
								  end;
							  _->Acc
						  end
				  end,
			lists:foldl(Fun, [], GroupList);
		_->skip
	end,
	ok.


%%获取本群房间列表
req_get_group_room_list(Aid,Sid,Seq)->
	case db:dirty_get(group_members, Aid, #group_members.accid) of
		GroupList when is_list(GroupList) andalso length(GroupList)>0->
			Fun = fun(#group_members{group_id=GroupId,state=1,accid=OAccId},Acc)->
						  case db:dirty_get(group_info,GroupId) of
							  [#group_info{group_name=GroupName}|_]->
								  case db:dirty_get(ply,OAccId) of
									  [#ply{room_id=RoomId}|_]->
										  [{RoomId,GroupId,GroupName,OAccId}]++Acc;
									  _->Acc
								  end;
							  _->Acc
						  end
				  end,
			case lists:foldl(Fun, [], GroupList) of
				GroupRoomList when is_list(GroupRoomList) andalso length(GroupRoomList) >0 ->
					Fun1 = fun({RoomId,GroupId,GroupName,OAccId},Acc)->
								   case lists:keyfind(RoomId, 1, Acc) of
									   false->
										   [{RoomId,GroupId,[OAccId]}]++Acc;
									   {RoomId,GroupId,GroupName,OAccIdList}->
										   lists:keyreplace(RoomId, 1, Acc, {RoomId,GroupId,GroupName,OAccIdList++[OAccId]})
								   end
						   end,
					case lists:foldl(Fun1, [], GroupRoomList) of
						RoomAccList when is_list(RoomAccList) andalso length(RoomAccList) >0->
							Fun2 = fun({RoomId,GroupId,GroupName,AccList},Acc)->
										   case db:dirty_get(room,RoomId) of
											   [#room{req=Req}|_]->
												   case fun_room:get_room_info(Req) of
													   [Times,Score,RoomRate,PlyMode,Option1,Option2]->
														   case fun_room:get_room_module_id(Req) of
															   ModelId when is_integer(ModelId) ->
																   {RoomId,GroupId,GroupName,length(AccList),ModelId,Times,Score,RoomRate,PlyMode,Option1,Option2};
															   _->Acc
														   end;
													   _->Acc
												   end;
											   _->Acc
										   end
								   end,
							case lists:foldl(Fun2,[],RoomAccList) of
								RoomGroupListInfo when is_list(RoomGroupListInfo)->
									Fun3 = fun({RoomId,GroupId,GroupName,Population,ModelId,Times,Score,RoomRate,PlyMode,Option1,Option2})->
												   Ptp = pt_public_class:group_list_info_new(),
												   Ptp#pt_public_group_list_info{room_id = RoomId,group_id = GroupId,group_name = GroupName,population = Population,model = ModelId,
																				 score = Score,times = Times,room_rate = RoomRate,ply_mode = PlyMode,option_1 = Option1,option_2 = Option2}  
										   end,
									NewInfo = lists:map(Fun3, RoomGroupListInfo),
									Pt = pt_group_list_d204:new(),
									Pt1 = Pt#pt_group_list{group_list_info = NewInfo},
									?send(Sid,pt_group_list_d204:to_binary(Pt1, Seq));
								_->skip
							end;
						_->skip
					end;	
				_->skip
			end;
		_->skip
	end.








